package repertory.android.ericchen.androidrepertory.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import repertory.android.ericchen.androidrepertory.R;

/**
 * className:
 * author: Ericchen
 * description:
 * date: 2017/3/8 14:11
 * update: 2017/3/8
 * version:
 */
public abstract class  PopupDialog extends Dialog{
    protected int mScreenWidth;

    private boolean mDismissOnClick;

    private int windowAnimations;

    private Window mWindow;

    public PopupDialog(Context context) {
        super(context, R.style.PopMenu);

        final WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        setCanceledOnTouchOutside(true);

        initializeDefault();

        mScreenWidth = windowManager.getDefaultDisplay().getWidth();
        mWindow = this.getWindow();
    }

    private void initializeDefault() {
        mDismissOnClick = true;
        windowAnimations = R.style.DialogShowStyle;
    }

    public void setDismissOnClick(boolean dismissOnClick) {
        mDismissOnClick = dismissOnClick;
    }

    public boolean getDismissOnClick() {
        return mDismissOnClick;
    }

    public void showFromBottom() {
        if (isShowing()) {
            dismiss();
        } else {
            onShow();
            WindowManager.LayoutParams Params = mWindow.getAttributes();
            Params.windowAnimations = windowAnimations;
            Params.width = mScreenWidth;
            Params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            mWindow.setAttributes(Params);
            mWindow.setGravity(Gravity.BOTTOM);
            super.show();
        }
    }

    /**
     * 显示之前调用
     */
    protected abstract void onShow();

    /**
     * 消失之前调用
     */
    protected abstract void onDismiss();

    public void dismissToBottom() {
        onDismiss();
        super.dismiss();
    }
}
